#!/bin/bash
# Ubuntu Bionic (18.04) - Ros Melodic
# Author: liujin

setup_color() {
	# Only use colors if connected to a terminal
	if [ -t 1 ]; then
		RED=$(printf '\033[31m')
		GREEN=$(printf '\033[32m')
		YELLOW=$(printf '\033[33m')
		BLUE=$(printf '\033[34m')
		BOLD=$(printf '\033[1m')
		RESET=$(printf '\033[m')
	else
		RED=""
		GREEN=""
		YELLOW=""
		BLUE=""
		BOLD=""
		RESET=""
	fi
}
setup_color

############################
# -- 0. Set Environment -- #
############################
rc_file=$([[ "$SHELL" =~ .*zsh ]] && echo "$HOME/.zshrc" || echo "$HOME/.bashrc")
sh_type=$([[ "$SHELL" =~ .*zsh ]] && echo "zsh" || echo "bash")

user_name=$(whoami)
name_ros_distro="melodic"  # ros version
# Getting version and release number of Ubuntu
version=$(lsb_release -sc)
relesenum=$(grep DISTRIB_DESCRIPTION /etc/*-release | awk -F 'Ubuntu ' '{print $2}' | awk -F ' LTS' '{print $1}')
echo "${GREEN}>>> Your Ubuntu version is: [Ubuntu $version $relesenum]${RESET}"


############################
# -- 1. Prepare Sources -- #
############################
# 添加清华 ROS 镜像源到 sources.list.d, 并添加 GPG Key, 参考 https://mirrors.tuna.tsinghua.edu.cn/help/ros/
echo "\n${GREEN}>>> Step 1. 添加清华 ROS 镜像源${RESET}\n"
sudo mkdir -p "/etc/apt/sources.list.d"
sudo sh -c "echo \"deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu ${version} main\" > /etc/apt/sources.list.d/ros-latest.list"
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
echo ">>> Updating Ubuntu package index, this will take few minutes depend on your network connection"
sudo apt update


########################
# -- 2. Install ROS -- #
########################
echo "\n${GREEN}>>> Step 2. 安装 ROS ${name_ros_distro}${RESET}\n"

# Checking version is bionic, if yes proceed otherwise quit
if [[ $version != "bionic" ]]; then
    echo ">>> {ERROR: This script will only work on Ubuntu Bionic (18.04).}"
    exit 0
fi

# Pick install type
echo ">>> Please pick how much of ROS you would like to install.\n"
echo "     [1. Desktop-Full Install: (Recommended) : Everything in Desktop plus 2D/3D simulators and 2D/3D perception packages ]"
echo "     [2. Desktop Install: Everything in ROS-Base plus tools like rqt and rviz]"
echo "     [3. ROS-Base: (Bare Bones) ROS packaging, build, and communication libraries. No GUI tools.]"
read -p "Enter your install (Default is 1):" answer
case "$answer" in
  1)
    package_type="desktop-full"
    ;;
  2)
    package_type="desktop"
    ;;
  3)
    package_type="ros-base"
    ;;
  * )
    package_type="desktop-full"
    ;;
esac
echo ">>> Starting ROS installation, this will take about 20 min. It will depends on your internet connection"
sudo apt install -y ros-${name_ros_distro}-${package_type}
# 将配置写入 rc 文件
echo ">>> Add ROS environment to [.bashrc/.zshrc]"
echo "source /opt/ros/${name_ros_distro}/setup.${sh_type}" >> $rc_file
source $rc_file


##############################################
# -- 3. Install Dependencies for building -- #
##############################################

echo "\n>>> ${GREEN}Step 3. 安装构建依赖, 配置 rosdep${RESET}"
# install this tool and other dependencies for building ROS packages
sudo apt install -y python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential curl

# 添加 Rosdep 清华源, 将源的位置写入环境变量, 参考https://mirrors.tuna.tsinghua.edu.cn/help/rosdistro/
echo ">>> ${BLUE}添加清华 Rosdep 镜像源${RESET}"
sudo mkdir -p /etc/ros/rosdep/sources.list.d/
sudo curl -o /etc/ros/rosdep/sources.list.d/20-default.list https://mirrors.tuna.tsinghua.edu.cn/github-raw/ros/rosdistro/master/rosdep/sources.list.d/20-default.list

# rosdep 更新包索引
echo 'export ROSDISTRO_INDEX_URL=https://mirrors.tuna.tsinghua.edu.cn/rosdistro/index-v4.yaml' >> $rc_file
export ROSDISTRO_INDEX_URL=https://mirrors.tuna.tsinghua.edu.cn/rosdistro/index-v4.yaml
rosdep update


echo "\n>>> ${GREEN}Step 4: Testing ROS installation, checking ROS version.${RESET}\n"
echo ">>>     Type 'rosversion -d' to get the current ROS installed version."
echo ">>>     You can install packages using 'rosdep install <packages>'."